package com.sskj.award.ui.activity;

import android.annotation.SuppressLint;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.award.R;
import com.sskj.award.R2;
import com.sskj.award.bean.WelfareDistrictBean;
import com.sskj.award.component.DaggerUserDataComponent;
import com.sskj.award.presenter.WelfareDistrictPrestener;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.NumberUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 福利区
 */
@Route(path = RConfig.SUPER_WELFARE_DISTRICT)
public class WelfareDistrictActivity extends BaseActivity<WelfareDistrictPrestener> {
    @BindView(R2.id.tvShowTotalFree)
    TextView tvShowTotalFree;
    @BindView(R2.id.tvShowNextDividentTime)
    TextView tvShowNextDividentTime;
    @BindView(R2.id.tvShowNextStartTime)
    TextView tvShowNextStartTime;
    @BindView(R2.id.tvSuperName)
    TextView tvSuperName;
    @BindView(R2.id.tvSuperMoney)
    TextView tvSuperMoney;
    @BindView(R2.id.btBuySuperPlatform)
    Button btBuySuperPlatform;
    @BindView(R2.id.tvShowTypeNext)
    TextView tvShowTypeNext;
    @BindView(R2.id.tvSuperMoneyNext)
    TextView tvSuperMoneyNext;
    @BindView(R2.id.btSuperRecord)
    Button btSuperRecord;
    @BindView(R2.id.tvTradeWarriorsName)
    TextView tvTradeWarriorsName;
    @BindView(R2.id.tvTitleTradeWarriorsMoney)
    TextView tvTitleTradeWarriorsMoney;
    @BindView(R2.id.tvTradeWarriorsMoney)
    TextView tvTradeWarriorsMoney;
    @BindView(R2.id.tvTradeWarriorsTime)
    TextView tvTradeWarriorsTime;
    @BindView(R2.id.btTradeWarriors)
    Button btTradeWarriors;
    @BindView(R2.id.btTradeWarriorsRecord)
    Button btTradeWarriorsRecord;
    @BindView(R2.id.tvShowTypeName)
    TextView tvShowTypeName;
    @BindView(R2.id.tvTitleWiseTradingMoney)
    TextView tvTitleWiseTradingMoney;
    @BindView(R2.id.tvWiseTradingMoney)
    TextView tvWiseTradingMoney;
    @BindView(R2.id.tvWiseTradingTime)
    TextView tvWiseTradingTime;
    @BindView(R2.id.btWiseTrading)
    Button btWiseTrading;
    @BindView(R2.id.btWiseTradingRecord)
    Button btWiseTradingRecord;
    @BindView(R2.id.llNoState)
    LinearLayout llNoState;
    @BindView(R2.id.llYesState)
    LinearLayout llYesState;
    private AlertDialog buyDialog;
    private AlertDialog.Builder builder;
    private String type;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.super_layout_activity_welfare_district;
    }

    @Override
    public WelfareDistrictPrestener getPresenter() {
        return new WelfareDistrictPrestener();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.super_welfareDistrictActivity1));
        DaggerUserDataComponent.create().inject(this);
        //超级平台弹出框
        ClickUtil.click(300, btBuySuperPlatform, () -> {
            showSuperBuyDialog();
        });
        //超级平台分红记录
        ClickUtil.click(300, btSuperRecord, () -> {
            ARouter.getInstance().build(RConfig.SUPER_SUPER_ABONUS_RECORD_DROP).navigation();
        });

    }

    @Override
    protected void initData() {
        mPresenter.getDistrictData();
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                if (userData.getSuper_user() == 0) {
                    llYesState.setVisibility(View.GONE);
                }else {
                    llNoState.setVisibility(View.GONE);
                }
            } else {
                userData = null;
            }
        });
    }

    @Override
    protected void initEvent() {
        super.initEvent();

    }

    //购买超级平台弹出框
    @SuppressLint("SetTextI18n")
    private void showSuperBuyDialog() {
        if (buyDialog == null) {
            builder = new AlertDialog.Builder(this);
            View view = LayoutInflater.from(this).inflate(R.layout.super_buy_dialog_next, null, false);
            builder.setView(view);
            buyDialog = builder.create();
            buyDialog.setCanceledOnTouchOutside(false);
            TextView etBuyMoney = view.findViewById(R.id.superDialogMoney);
            if (userData == null) {
                return;
            }
            etBuyMoney.setText(userData.getUpgradeSuperUserMoney() + "USDT");
            EditText etBuyPwd = view.findViewById(R.id.etBuyPwd);
            TextView tvCancel = view.findViewById(R.id.tvCancel);
            TextView tvConfirm = view.findViewById(R.id.tvConfirm);
            ClickUtil.click(tvCancel, () -> {
                if (buyDialog != null && buyDialog.isShowing()) {
                    buyDialog.dismiss();
                    buyDialog = null;
                }
            });
            ClickUtil.click(tvConfirm, () -> {
                String buyPwd = etBuyPwd.getText().toString().trim();
                if (TextUtils.isEmpty(buyPwd)) {
                    Toast.makeText(mActivity, App.INSTANCE.getString(R.string.super_buy_hint_pwd), Toast.LENGTH_SHORT).show();
                    return;
                }
                // TODO: 2019/7/12 调用接口
                mPresenter.upDataSuperUser(etBuyPwd.getText().toString());
                if (buyDialog != null && buyDialog.isShowing()) {
                    buyDialog.dismiss();
                    buyDialog = null;
                }
            });
            Window window = buyDialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
            buyDialog.show();
        }
    }

    //购买交易勇士弹出框
    private void showBuyDialog() {
        if (buyDialog == null) {
            builder = new AlertDialog.Builder(this);
            View view = LayoutInflater.from(this).inflate(R.layout.super_buy_dialog, null, false);
            builder.setView(view);
            buyDialog = builder.create();
            buyDialog.setCanceledOnTouchOutside(false);
            EditText etBuyCount = view.findViewById(R.id.etBuyCount);
            EditText etBuyPwd = view.findViewById(R.id.etBuyPwd);
            TextView tvCancel = view.findViewById(R.id.tvCancel);
            TextView tvConfirm = view.findViewById(R.id.tvConfirm);
            ClickUtil.click(tvCancel, () -> {
                if (buyDialog != null && buyDialog.isShowing()) {
                    buyDialog.dismiss();
                    buyDialog = null;
                }
            });
            ClickUtil.click(tvConfirm, () -> {
                String buyCount = etBuyCount.getText().toString().trim();
                if (TextUtils.isEmpty(buyCount)) {
                    Toast.makeText(mActivity, App.INSTANCE.getString(R.string.super_buy_hint_count), Toast.LENGTH_SHORT).show();
                    return;
                }
                String buyPwd = etBuyPwd.getText().toString().trim();
                if (TextUtils.isEmpty(buyPwd)) {
                    Toast.makeText(mActivity, App.INSTANCE.getString(R.string.super_buy_hint_pwd), Toast.LENGTH_SHORT).show();
                    return;
                }
                // TODO: 2019/7/12 调用接口
                mPresenter.onCreateParticipate("SOP", etBuyPwd.getText().toString(), etBuyCount.getText().toString(), type);
                if (buyDialog != null && buyDialog.isShowing()) {
                    buyDialog.dismiss();
                    buyDialog = null;
                }
            });
            Window window = buyDialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
            buyDialog.show();
        }
    }

    //手续费福利区数据获取
    @SuppressLint("SetTextI18n")
    public void getDistrictDataSuccess(WelfareDistrictBean data) {
        if (data == null) {
            return;
        }
        tvShowTotalFree.setText(NumberUtil.keepNoZero(NumberUtil.keep(data.getTotal_handling_fee(),6))+"USDT");
        tvShowNextDividentTime.setText(data.getNext_dividend_time());
        tvShowNextStartTime.setText(data.getNext_participate_time());
        tvSuperMoney.setText(NumberUtil.keepNoZero(NumberUtil.keep(data.getSuper_handling_fee(),6)) + "USDT");
        tvSuperMoneyNext.setText(NumberUtil.keepNoZero(NumberUtil.keep(data.getSuper_handling_fee(),6)) + "USDT");

//        ClickUtil.click(300, btBuySuperPlatform, () -> {
//            showBuyDialog();
//        });
        ClickUtil.click(300, btTradeWarriors, () -> {
            type = "1";
            showBuyDialog();
        });
        ClickUtil.click(300, btWiseTrading, () -> {
            type = "2";
            showBuyDialog();
        });

        ClickUtil.click(300, btSuperRecord, () -> {
            ARouter.getInstance().build(RConfig.SUPER_SUPER_ABONUS_RECORD_DROP).withString(Constans.TYPE, "001").navigation();
        });

        ClickUtil.click(300, btTradeWarriorsRecord, () -> {
            ARouter.getInstance().build(RConfig.SUPER_DIVIDEND_RECORD).withString(Constans.ACCOUNT, "1").withString(Constans.TYPE, "002").navigation();
        });

        ClickUtil.click(300, btWiseTradingRecord, () -> {
            ARouter.getInstance().build(RConfig.SUPER_DIVIDEND_RECORD).withString(Constans.ACCOUNT, "2").withString(Constans.TYPE, "002").navigation();
        });

        //勇士
        tvTitleTradeWarriorsMoney.setText(NumberUtil.keepNoZero(NumberUtil.keep(data.getParticipate_yongshi().getTotal_handling_fee(),6))+"USDT");
        tvTradeWarriorsMoney.setText(App.INSTANCE.getString(R.string.super_welfareDistrictActivity4) + NumberUtil.keepNoZero(NumberUtil.keep(data.getParticipate_yongshi().getMoney(),6))+"USDT");
        tvTradeWarriorsTime.setText(App.INSTANCE.getString(R.string.super_welfareDistrictActivity5) + data.getParticipate_yongshi().getLocked_days() + App.INSTANCE.getString(R.string.super_welfareDistrictActivity6));
        //智者
        tvTitleWiseTradingMoney.setText(NumberUtil.keepNoZero(NumberUtil.keep(data.getParticipate_zhizhe().getTotal_handling_fee(),6))+"USDT");
        tvWiseTradingMoney.setText(App.INSTANCE.getString(R.string.super_welfareDistrictActivity4) + NumberUtil.keepNoZero(NumberUtil.keep(data.getParticipate_zhizhe().getMoney(),6))+"USDT");
        tvWiseTradingTime.setText(App.INSTANCE.getString(R.string.super_welfareDistrictActivity5) + data.getParticipate_zhizhe().getLocked_days() + App.INSTANCE.getString(R.string.super_welfareDistrictActivity6));
    }

    //勇者和智者交易成功
    public void onCreateParticipateSuccess() {
        ToastUtil.showShort(App.INSTANCE.getString(R.string.super_welfareDistrictActivity7));
        ARouter.getInstance().build(RConfig.SUPER_DIVIDEND_RECORD).withString(Constans.ACCOUNT, "1").withString(Constans.TYPE, "002").navigation();
    }

    //超级平台升级成功
    public void upDataSuperUserSuccess() {
        ToastUtil.showShort(App.INSTANCE.getString(R.string.super_superPlatformActivity15));
    }
}
